# ~*~ coding: utf-8 ~*~
import time

from sqlalchemy_mptt.mixins import BaseNestedSets
from sqlalchemy.dialects.mysql import TINYINT

from apps.common.dbbase import BaseModel
from apps.common.dbtypes import JsonDictMediumTextField
from apps.common.utils import generate_uuid
from apps.extensions import db

__all__ = ['Role']


class Role(BaseModel):
    __tablename__ = 'tasks_realtime_role'

    id = db.Column(db.String(36), default=generate_uuid, primary_key=True)
    name = db.Column(db.String(128), unique=True)
    code = db.Column(db.String(32), unique=True)
    desc = db.Column(db.TEXT)

    create_by = db.Column(db.VARCHAR(32), nullable=True)
    create_time = db.Column(db.INT, default=time.time)
    update_time = db.Column(db.INT, default=time.time, onupdate=time.time)

    def __repr__(self):
        return "<Tree (%s)>" % self.name


class RoleExecuteLog(BaseModel):
    __tablename__ = 'tasks_realtime_role_log'

    id = db.Column(db.String(36), default=generate_uuid, primary_key=True)
    celery_id = db.Column(db.String(36))
    user = db.Column(db.String(128))
    remote_ip = db.Column(db.String(15))
    playbook = db.Column(db.TEXT)
    hosts = db.Column(db.TEXT)
    groups = db.Column(db.TEXT)
    codes = db.Column(db.TEXT)
    result = db.Column(JsonDictMediumTextField, nullable=True, default={})
    exec_time = db.Column(db.INT, default=time.time)

